package com.goldstone.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.goldstone.dao.TagDao;
import com.goldstone.entity.Tag;

@Component
@Transactional
public class TagService {
	@Autowired
	private TagDao dao;
	
	public void save(Tag tag) {
		Tag t = dao.findUniqueBy("name", tag.getName());
		if(t == null) {
			dao.save(tag);
		} else {
			tag = t;
		}
	}
	
	public List<Tag> saveTagString(String tagString) {
		List<Tag> tags = Tag.getTags(tagString);
		List<Tag> tag1s = new ArrayList<Tag>();
		for(Tag tag : tags) {
			Tag t = dao.findUniqueBy("name", tag.getName());
			if(t == null) {
				dao.save(tag);
				tag1s.add(tag);
			} else {
				tag1s.add(t);
			}
		}
		return tag1s;
	}
	
	@Transactional(readOnly = true)
	public Tag get(String id) {
		return dao.get(id);
	}
	@Transactional(readOnly = true)
	public void find(Page<Tag> page, List<PropertyFilter> filters) {
		dao.findPage(page, filters);
	}
	public void delete(String id) {
		dao.delete(id);
	}
	
}
